<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
    {{user}}
      <el-form :model="user">
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select
            v-model="user.pid"
            placeholder="请选择上级菜单"
            @change="changePid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in arr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 如果是目录，那么有图标，没有地址，如果是菜单，有地址，没有图标 -->
        <el-form-item label="菜单类型" label-width="120px">
          <el-radio v-model="user.type" disabled :label="1">目录</el-radio>
          <el-radio v-model="user.type" disabled :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item
          label="菜单图标"
          label-width="120px"
          v-if="user.type == 1"
        >
          <el-select v-model="user.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" label-width="120px" v-else>
          <el-select v-model="user.url" placeholder="请选择菜单地址">
            <el-option
              v-for="item in IndexRoutes"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="user.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd">添 加</el-button>
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { menuadd, menuedit, menuinfo } from '../../../http/api';
import { IndexRoutes } from "../../../router";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info", "arr"],
  data() {
    return {
      isshow: true,
      user: {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 2,
      },
      // 字体图标
      icons: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-help",
        "el-icon-camera-solid ",
        "el-icon-s-platform",
      ],
      //二级路由
      IndexRoutes
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({}),
    //取消
    cancel(){
      this.info.isshow=false
    },
    //清空
    empty(){
      this.user={
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 2,
      }
    },

    changePid(){
      //如果pid是0，那么type为1、目录，否则type为2，菜单
      if(this.user.pid==0){
        this.user.type=1;
      }else{
        this.user.type=2
      }
    },
    add(){
      menuadd(this.user).then(res=>{
        if(res.data.code==200){
          successAlert(res.data.msg);
          this.info.isshow=false;
          this.empty();
          //通知父组件刷新表格
          this.$emit("init")
        }
      })
    },
    //获取一个数据
    getOne(id){
      menuinfo({id}).then(res=>{
        if(res.data.code==200){
          this.user=res.data.list;
          //补id字段,发送修改时需要使用
          this.user.id=id
        }
      })
    },
    update(){
      menuedit(this.user).then(res=>{
        if(res.data.code=200){
          successAlert(res.data.msg);
          this.info.isshow=false;
          this.empty();
          //通知父组件刷新表格
          this.$emit("init");
        }
      })
    },
    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed(){
      if(!this.info.isAdd){
        this.empty()
      }
    }
  },
};
</script>

<style>
</style>